/**
 * Project: lionbule-java-test
 * 
 * File Created at 2011-10-9
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package org.log.logback;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;

/**
 * TestAppender.
 * 2011-10-9 下午03:01:48 @author LionBule
 */
public class TestAppender {

    public static void main(String[] args) {
        Logger logger = LoggerFactory.getLogger("test");  
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();  
          
        ch.qos.logback.classic.Logger newLogger = (ch.qos.logback.classic.Logger)logger;  
        //Remove all previously added appenders from this logger instance.  
        newLogger.detachAndStopAllAppenders();  
          
        //define appender  
        RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<ILoggingEvent>();  
          
        //policy  
        TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>();  
        policy.setContext(loggerContext);  
        policy.setMaxHistory(5);  
        policy.setFileNamePattern("/home/lionbule/%d{yyyy-MM-dd}.log");  
        policy.setParent(appender);  
        policy.start();  
          
        //encoder  
        PatternLayoutEncoder encoder = new PatternLayoutEncoder();  
        encoder.setContext(loggerContext);  
        encoder.setPattern("%message%n");  
        encoder.start();  
        
        //start appender  
        appender.setRollingPolicy(policy);  
        appender.setContext(loggerContext);  
        appender.setEncoder(encoder);  
        appender.setPrudent(true); //support that multiple JVMs can safely write to the same file.  
        appender.start();  
          
        newLogger.addAppender(appender);  
          
        //setup level  
        newLogger.setLevel(Level.WARN);  
        //remove the appenders that inherited 'ROOT'.  
        newLogger.setAdditive(false);
        
        newLogger.warn("test");
        System.out.println("teststs");
    }

}
